package com.ny.view;

import com.ny.entity.Order;
import com.ny.entity.Product;
import com.ny.entity.Stud;
import com.ny.service.OrderService;
import com.ny.service.ProductService;
import com.ny.service.StudentService;
import com.ny.service.impl.OrderServiceImpl;
import com.ny.service.impl.ProductServiceImpl;
import com.ny.service.impl.StudentServiceImpl;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class InterView {
    ProductService productService = new ProductServiceImpl();
    OrderService  orderService = new OrderServiceImpl();
    StudentService studentService = new StudentServiceImpl();
    public InterView() throws SQLException, ClassNotFoundException {
    }

    public  void indexview(Stud stud) throws SQLException, ClassNotFoundException {
        boolean f = true;
        while (f) {
            System.out.println("请选择你的操作");
            System.out.println("1.个人信息内容界面");
            System.out.println("2.商品信息界面");
            System.out.println("3.订单界面");
             System.out.println("4.修改密码");
            System.out.println("5.修改手机号码");
            System.out.println("6.退出");
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("1.个人信息内容界面");
                    System.out.println("学生id：" + stud.getStuID());
                    System.out.println("用户名：" + stud.getStuName());
                    System.out.println("手机号：" + stud.getStuPhone());
                    System.out.println("密码：" + stud.getStuPwd());
                    System.out.println("4.修改密码");
                    System.out.println("5.修改手机号码");
                    int choice2 = scanner.nextInt();
                    switch (choice2) {
                        case 4:
                            System.out.println("4.修改密码");
                            String name = stud.getStuName();
                            System.out.println("请输入旧密码");
                            String pwd = scanner.next();
                            if (pwd.equals(stud.getStuPwd())) {
                                System.out.println("请输入新密码");
                                String newpwd = scanner.next();


                                Stud stud1 = studentService.changePwd(name, pwd);
                                if (stud1 != null) {
                                    stud1.setStuPwd(newpwd);
                                    studentService.changePwd(name, newpwd);
                                    System.out.println("修改成功");
                                } else {
                                    System.out.println("修改失败");

                                }
                            } else {
                                System.out.println("旧密码错误,无法修改");
                            }
                            break;
                        case 5:
                            System.out.println("请输入旧手机号");
                            String phone = scanner.next();
                            if (phone.equals(stud.getStuPhone())) {
                                System.out.println("请输入新手机号");
                                String newphone = scanner.next();
                            }
                            break;
                    }
                    break;
                case 2:
                    System.out.println("2.商品信息界面");
                    List<Product> list = productService.findAllProduct();
                    System.out.println("商品id" + "\t" + "\t" + "商品名称" + "\t" + "商品价格" + "\t" + "商品库存" + "\t" + "商品类别" + "\t" + "商品描述");
                    for (Product product : list) {
                        System.out.print(product.getProduct_id());
                        System.out.print("\t" + "\t");
                        System.out.print(product.getName());
                        System.out.print("\t" + "\t");
                        System.out.print(product.getPrice());
                        System.out.print("\t" + "\t");
                        System.out.print(product.getStock());
                        System.out.print("\t" + "\t");
                        System.out.print(product.getCategory());
                        System.out.print("\t" + "\t");
                        System.out.println(product.getDescription());
                    }
                    System.out.println("输入购买商品序号");
                    int choice3 = scanner.nextInt();
                    System.out.println("2.返回");
                    switch (choice3) {
                        case 1:
                            Stud stud1 = new Stud();
                            System.out.println("输入购买商品序号");
                            int choice1 = scanner.nextInt();
                            System.out.println("输入购买数量");
                            int num = scanner.nextInt();
                            int stuid = stud.getStuID();
                            Product product = productService.findAllProduct().get(choice1 - 1);
                            double total_price = productService.findAllProduct().get(choice1 - 1).getPrice() * num;


                            //如果购买数量大于库存数量，则提示购买失败，否则购买成功，并编辑数据库的库存数量
                            if (num > product.getStock()) {
                                System.out.println("购买失败");
                            } else if (num <= product.getStock()) {
                                System.out.println("购买成功");
                                Order order = new Order();
                                order.setStudent_id(stuid);
                                order.setProduct_id(choice1);
                                order.setNumber(num);
                                order.setTotal_price(total_price);
                                System.out.println("准备插入订单，student_id: " + order.getStudent_id());
                                System.out.println("product_id: " + order.getProduct_id());
                                System.out.println("number: " + order.getNumber());
                                System.out.println("total_price: " + order.getTotal_price());
                                productService.reduceStock(product.getProduct_id(), num);
                                orderService.addOrder(order);

                            }
                        case 2:
                            indexview(stud);
                    }
                    break;

                case 3:
                    System.out.println("3.订单界面");
                    List<Order> list1 = orderService.findAllOrder();
                    System.out.println("订单id"  + "\t" + "商品id" + "\t" + "商品数量" + "\t" + "总价");
                    for (Order order : list1) {
                        System.out.print(order.getOrder_id());
                        System.out.print("\t" + "\t");
                        System.out.print(order.getProduct_id());
                        System.out.print("\t" + "\t");
                        System.out.print(order.getNumber());
                        System.out.print("\t" + "\t");
                        System.out.println(order.getTotal_price());

                    }
                    System.out.println("1.返回");
                    int choice4 = scanner.nextInt();
                    switch (choice4) {
                        case 1:
                            indexview(stud);
                            break;
                    }
                    break;
                        case 6:
                            System.out.println("6.退出用户界面");
                            ViewMain viewMain = new ViewMain();
                            viewMain.main(null);
                            break;

                    }



                }
            }

    }
